<template>
  <div>
    <el-card>
        <div style="margin-bottom: 10px">
          <el-input style="width: 200px;margin: 0 5px" placeholder="查询..." v-model="name"></el-input>
          <el-button type="success" plain @click="load(1)">查询</el-button>
          <el-button type="info" plain @click="reset">重置</el-button>
          <el-button type="primary" plain @click="handleAdd">新增</el-button>
        </div>
        <el-table :data="tableData" stripe>
          <el-table-column prop="id" label="序号" width="70" align="center" >
            <template slot-scope='scope'>
              <span>{{ (pageNum - 1) * pageSize + (scope.$index + 1) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="name" label="名称" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column align="center" prop="descr" label="简介" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column align="center" prop="content" label="内容">
            <template slot-scope="scope">
              <el-button plain type="success" @click="view(scope.row)" size="mini">预览</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="图片"><template v-slot="scope"><el-image style="width: 50px; height: 50px" :src="scope.row.cover" :preview-src-list="[scope.row.cover]"></el-image></template></el-table-column>
          <el-table-column align="center" prop="categoryId" label="类别ID">
            <template v-slot="scope">
              {{ categories.find(v => v.id === scope.row.categoryId)?.name || "未知类型" }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="author" label="作者"></el-table-column>
          <el-table-column align="center" prop="publisher" label="出版社"></el-table-column>
          <el-table-column align="center" prop="date" label="出版日期"></el-table-column>
          <el-table-column align="center" prop="isbn" label="ISBN"></el-table-column>
          <el-table-column align="center" prop="store" label="库存"></el-table-column>
          <el-table-column align="center" prop="isShelf" label="上下架" width="200">
            <template v-slot="scope">
              <el-switch
                  @change="(newState) => handleSwitchChange(scope.row, newState)"
                  v-model="scope.row.isShelf"
                  active-text="上架"
                  inactive-text="下架"
                  active-value="0"
                  inactive-value="1">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="240" align="center">
            <template slot-scope="scope">
              <el-button plain type="success" @click="detail(scope.row)" size="mini">详情</el-button>
              <el-button plain type="primary" @click="handleEdit(scope.row)" size="mini">编辑</el-button>
              <el-button plain type="danger" size="mini" @click=del(scope.row.id)>删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div style="margin: 10px 0">
          <el-pagination
              style="padding: 0"
              background
              layout="total, prev, pager, next"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-size="pageSize"
              :total="total">
          </el-pagination>
        </div>
    </el-card>

    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="50%" :close-on-click-modal="false">
      <el-form label-width="100px" style="padding-right: 40px" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item prop="name" label="名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="descr" label="简介">
          <el-input type="textarea" v-model="form.descr" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="content" label="内容">
          <div id="editor"></div>
        </el-form-item>
        <el-form-item prop="cover" label="封面">
          <el-upload :action="$baseUrl +'/file/upload'" :headers="{ token: user.token }" ref="img" :on-success="handleImgUploadSuccess">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item prop="categoryId" label="类别">
          <el-select v-model="form.categoryId" placeholder="请选择分类">
            <el-option v-for="item in categories" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="author" label="作者">
          <el-input v-model="form.author" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="publisher" label="出版社">
          <el-input v-model="form.publisher" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="date" label="出版日期">
          <el-date-picker style='width:100%' v-model="form.date" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item prop="isbn" label="ISBN">
          <el-input v-model="form.isbn" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="store" label="库存">
          <el-input v-model="form.store" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <el-drawer :visible.sync="drawerFormVisible" title="详情" :with-header="false">
      <div class="drawer-header">
        <span class="drawer-title">详情</span>
        <div class="drawer-actions">
          <el-tooltip placement="top" :content="isFullscreen ? '退出全屏' : '全屏'">
            <el-button icon="el-icon-full-screen" size="mini" circle @click="toggleFullscreen"/>
          </el-tooltip>
          <el-button icon="el-icon-close" size="mini" circle @click="drawerFormVisible = false"/>
        </div>
      </div>

      <!-- 抽屉内容 -->
      <div class="drawer-content" ref="drawerContent">
        <el-form label-width="100px" style="padding-right: 40px" :model="form">
          <el-form-item prop="name" label="名称">
            <div>{{form.name}}</div>
          </el-form-item>
          <el-form-item prop="descr" label="简介">
            <div>{{form.descr}}</div>
          </el-form-item>
          <el-form-item prop="content" label="内容">
            <div v-html="form.content"></div>
          </el-form-item>
          <el-form-item prop="cover" label="封面">
            <div>
              <el-image style="width: 50px; height: 50px" :src="form.cover" :preview-src-list="[form.cover]"></el-image>
            </div>
          </el-form-item>
          <el-form-item prop="categoryId" label="类别">
            <div>{{ categories.find(v => v.id === form.categoryId)?.name || "未知类型" }}</div>
          </el-form-item>
          <el-form-item prop="author" label="作者">
            <div>{{form.author}}</div>
          </el-form-item>
          <el-form-item prop="publisher" label="出版社">
            <div>{{form.publisher}}</div>
          </el-form-item>
          <el-form-item prop="date" label="出版日期">
            <div>{{form.date}}</div>
          </el-form-item>
          <el-form-item prop="isbn" label="ISBN">
            <div>{{form.isbn}}</div>
          </el-form-item>
          <el-form-item prop="store" label="库存">
            <div>{{form.store}}</div>
          </el-form-item>
          <el-form-item prop="isShelf" label="上下架">
            <div v-if="form.isShelf == 0">上架</div>
            <div v-else>下架</div>
          </el-form-item>
        </el-form>
      </div>

      <!-- 抽屉底部 -->
      <div class="drawer-footer">
        <el-button @click="drawerFormVisible = false">关闭</el-button>
      </div>
    </el-drawer>

    <el-dialog title="图书介绍" :visible.sync="dialogFormVisible1" width="60%">
      <div class="w-e-text" v-html="content"></div>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor"

export default {
  name: "Book",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      dialogFormVisible: false,
      drawerFormVisible: false,
      dialogFormVisible1: false,
      editor: null,
      content: '',
      categories: [],
      state: 0,
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      rules: {
        name: [
          { required: true, message: '请输入必填项', trigger: 'blur'}
        ],
      },
      isFullscreen: false,
      drawerWidth: '50%', // 默认抽屉宽度
      drawerPosition: 'right' // 默认抽屉位置
    }
  },
  created() {
    this.load()
  },
  methods: {
    handleSwitchChange(data,value){
      data.isShelf = value
      this.$request({
        method: 'PUT',
        url: "/book",
        data: data
      }).then(res => {
        if (res.code === '200') {
          this.$notify.success({title: '成功', message: '操作成功', showClose: false, duration: 2000});
          this.load()
        } else {
          this.$notify.success({title: '成功', message: res.msg, showClose: false, duration: 2000});
        }
      })
    },
    setEditor() {
      this.$nextTick(() => {
        this.editor = new E(`#editor`)
        this.editor.config.uploadImgHeaders = {
          token: this.user.token
        }
        this.editor.config.uploadImgServer = 'http://localhost:9999/file/editor/upload'
        this.editor.config.uploadFileName = 'file'
        this.editor.config.uploadImgHeaders = {
          token: this.user.token
        }
        this.editor.config.uploadVideoServer = 'http://localhost:9999/file/editor/uploadVideo'
        this.editor.config.uploadVideoName = 'file'
        this.editor.config.uploadVideoHeaders = {
          token: this.user.token
        }
        this.editor.create()
      })
    },
    view(row) {
      this.content = row.content
      this.dialogFormVisible1 = true
    },
    destroy() {
      this.editor.destroy()
      this.editor = null
    },
    load() {
      this.$request.get("/book/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: 10,
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data?.records
        this.total = res.data?.total
      })

      this.$request.get("/category").then(res => {
        this.categories = res.data
      })
    },
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.form.content = this.editor.txt.html()
          this.$request({
             method: this.form.id ? 'PUT' : 'POST',
             url: "/book",
             data: this.form
          }).then(res => {
            if (res.code === '200') {
              this.$notify.success({title: '成功', message: '操作成功', showClose: false, duration: 2000});
              this.dialogFormVisible = false
              this.load()
              this.destroy()
            } else {
              this.$notify.error({title: '成功', message: res.msg, showClose: false, duration: 2000});
            }
          })
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        // 设置富文本
        this.setEditor()
        if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
        if(this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
      this.$nextTick(() => {
        // 设置富文本
        this.setEditor()
        setTimeout(() => {
          this.editor.txt.html(row.content) //设置内容
        }, 0)
        if(this.$refs.img) {
           this.$refs.img.clearFiles();
        }
        if(this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    detail(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.drawerFormVisible = true
    },
    del(id) {
      this.$confirm('您确认删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
       this.$request.delete("/book/" + id).then(res => {
          if (res.code === '200') {
            this.$notify.success({title: '成功', message: '操作成功', showClose: false, duration: 2000});
            this.load()
          } else {
            this.$notify.error({title: '成功', message: res.msg, showClose: false, duration: 2000});
          }
        })
      }).catch(() => {})
    },
    reset() {
      this.name = ""
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleFileUploadSuccess(res) {
      this.form.file = res.data
    },
    handleImgUploadSuccess(res) {
      this.form.cover = res.data
    },
    handleFullscreenChange() {
      // 如果用户通过ESC键退出全屏，同步更新状态
      if (!document.fullscreenElement && this.isFullscreen) {
        this.isFullscreen = false;
        this.drawerWidth = this.originalWidth;
        this.drawerPosition = this.originalPosition;
      }
    },
    toggleFullscreen() {
      if (!this.isFullscreen) {
        // 保存当前抽屉状态以便退出全屏时恢复
        this.originalWidth = this.drawerWidth;
        this.originalPosition = this.drawerPosition;

        // 进入全屏模式
        this.isFullscreen = true;
        this.drawerWidth = '100%';
        this.drawerPosition = 'bottom';

        // 延迟执行全屏请求，确保DOM已更新
        this.$nextTick(() => {
          this.requestFullscreen(this.$refs.drawerContent);
        });
      } else {
        // 退出全屏模式
        this.isFullscreen = false;
        this.drawerWidth = this.originalWidth;
        this.drawerPosition = this.originalPosition;
        this.exitFullscreen();
      }
    },
    // 全屏API相关方法
    requestFullscreen(element) {
      if (element.requestFullscreen) {
        element.requestFullscreen();
      } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen();
      } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
      } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
      }
    },
    exitFullscreen() {
      if (document.exitFullscreen) {
        document.exitFullscreen();
      } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
      } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
      }
    }
  },
  // 监听全屏状态变化
  mounted() {
    document.addEventListener('fullscreenchange', this.handleFullscreenChange);
    document.addEventListener('webkitfullscreenchange', this.handleFullscreenChange);
    document.addEventListener('mozfullscreenchange', this.handleFullscreenChange);
    document.addEventListener('MSFullscreenChange', this.handleFullscreenChange);
  },
  beforeDestroy() {
    document.removeEventListener('fullscreenchange', this.handleFullscreenChange);
    document.removeEventListener('webkitfullscreenchange', this.handleFullscreenChange);
    document.removeEventListener('mozfullscreenchange', this.handleFullscreenChange);
    document.removeEventListener('MSFullscreenChange', this.handleFullscreenChange);
  },
}
</script>

<style scoped>

</style>
